package cn.lingyangwl.agile.pay.common.service.impl;

import cn.lingyangwl.agile.pay.common.assembly.PayWayAssembly;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import cn.lingyangwl.agile.common.core.exception.enums.GlobalErrorEnum;
import cn.lingyangwl.agile.pay.common.mapper.PayWayMapper;
import cn.lingyangwl.agile.pay.model.entity.MchPayPassage;
import cn.lingyangwl.agile.pay.model.entity.PayOrder;
import cn.lingyangwl.agile.pay.model.entity.PayWay;
import cn.lingyangwl.agile.pay.model.rqrs.PayWayQL;
import cn.lingyangwl.agile.pay.common.service.MchPayPassageService;
import cn.lingyangwl.agile.pay.common.service.PayOrderService;
import cn.lingyangwl.agile.pay.common.service.PayWayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Service
public class PayWayServiceImpl extends ServiceImpl<PayWayMapper, PayWay> implements PayWayService {
    @Resource
    private MchPayPassageService mchPayPassageService;
    @Resource
    private PayOrderService payOrderService;
    @Resource
    private PayWayAssembly payWayAssembly;

    @Override
    public IPage<PayWay> pagePayWay(PayWayQL query) {
        LambdaQueryWrapper<PayWay> lqw = PayWay.lqw()
                .like(StringUtils.isNotEmpty(query.getWayCode()), PayWay::getWayCode, query.getWayCode())
                .like(StringUtils.isNotEmpty(query.getWayName()), PayWay::getWayName, query.getWayName())
                .orderByDesc(PayWay::getCreatedAt);
        return this.page(PageUtil.getPage(), lqw);
    }

    @Override
    public void savePageWay(PayWay payWay) {
        if (this.count(PayWay.lqw().eq(PayWay::getWayCode, payWay.getWayCode())) > 0) {
            throw new BizException("支付方式代码已存在");
        }
        payWay.setWayCode(payWay.getWayCode().toUpperCase());

        boolean result = this.save(payWay);
        if (!result) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_CREATE);
        }
    }

    @Override
    public void deletePayWay(String wayCode) {
        // 校验该支付方式是否有商户已配置通道或者已有订单
        if (mchPayPassageService.count(MchPayPassage.lqw().eq(MchPayPassage::getWayCode, wayCode)) > 0
                || payOrderService.count(PayOrder.lqw().eq(PayOrder::getWayCode, wayCode)) > 0) {
            throw new BizException("该支付方式已有商户配置通道或已发生交易，无法删除！");
        }

        boolean result = this.removeById(wayCode);
        if (!result) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_DELETE);
        }
    }

    @Override
    public List<PayWay> listPayWay(PayWayQL query) {
        LambdaQueryWrapper<PayWay> lqw = PayWay.lqw()
                .like(StringUtils.isNotEmpty(query.getWayCode()), PayWay::getWayCode, query.getWayCode())
                .like(StringUtils.isNotEmpty(query.getWayName()), PayWay::getWayName, query.getWayName())
                .orderByDesc(PayWay::getCreatedAt);
        return this.list(lqw);
    }
}
